package com.kingsalert.kingsalert.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.bean.process.ProcessInfo;
import com.kingsalert.kingsalert.config.ProcessConfig;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.util.ArrayList;

public class ProcessBottomDialog extends BottomSheetDialog {

    private LinearLayout groupLayout;

    private final Runnable applyRunnable;

    private final String page = "DIALOG_PROCESS";

    public ProcessBottomDialog(Context context,Runnable applyRunnable){
        super(context, R.style.Theme_kingsalert_BottomDialogStyle);
        this.applyRunnable = applyRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_process);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        groupLayout = findViewById(R.id.dp_layoup_group);

        initProcessView();

        findViewById(R.id.dp_ib_close).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"CLOSE_CLICK","");
            dismiss();
        });

        findViewById(R.id.dp_btn_apply).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"APPLY_CLICK","");
            dismiss();
            if (applyRunnable!=null) applyRunnable.run();
        });

    }

    private void initProcessView(){
        groupLayout.removeAllViews();

        ArrayList<String> allSteps = ProcessInfo.getInstance().getAllSteps();
        ArrayList<String> steps = ProcessInfo.getInstance().getSteps();

        int i;
        for (i=0;i<allSteps.size();i++){
            String step = allSteps.get(i);
            String text = ProcessInfo.getInstance().getStepTitle(i);
            addProcess(ProcessConfig.getProcessImgResId(step),i+1,text,!steps.contains(step),false);
        }

        addProcess(R.drawable.img_process_dialog_submitloanneed,++i,getContext().getString(R.string.dialog_process_submitloanneed),false,false);
        addProcess(R.drawable.img_process_dialog_waitloan,++i,getContext().getString(R.string.dialog_process_waitloan),false,true);

    }

    private void addProcess(int imgResId,int num,String text,boolean isComplete,boolean isLast){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_process_dialog,null);

        if (num == 1) view.findViewById(R.id.ipd_view_topline).setVisibility(View.INVISIBLE);
        if (isLast) view.findViewById(R.id.ipd_view_bottomline).setVisibility(View.INVISIBLE);

        ImageView stateIv = view.findViewById(R.id.ipd_iv_state);
        ImageView typeIv = view.findViewById(R.id.ipd_iv_type);
        TextView stepNumTv = view.findViewById(R.id.ipd_tv_step_num);
        TextView textTv = view.findViewById(R.id.ipd_tv_text);

        if (isComplete) stateIv.setBackgroundResource(R.drawable.img_process_completed);
        stepNumTv.append(" "+(num<10 ? "0"+num:num));
        typeIv.setBackgroundResource(imgResId);
        textTv.setText(text);

        groupLayout.addView(view);
    }

}
